package com.example.myproject.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myproject.R;
import com.example.myproject.SideNavigation.ArborFragment;
import com.example.myproject.SideNavigation.FruitTreeFragment;
import com.example.myproject.SideNavigation.GrainCropsFragment;
import com.example.myproject.SideNavigation.MedicinalPlantFragment;
import com.example.myproject.SideNavigation.PestFragment;
import com.example.myproject.SideNavigation.VegetableFragment;
import com.example.myproject.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CollectFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CollectFragment extends Fragment implements View.OnClickListener {


    private View tab1,tab2,tab3,tab4,tab5,tab6;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private void changeTab(int positon){
        switch (positon){
            case 0:
                tab1.setBackgroundColor(Color.GRAY);
                tab2.setBackgroundColor(Color.WHITE);
                tab3.setBackgroundColor(Color.WHITE);
                tab4.setBackgroundColor(Color.WHITE);
                tab5.setBackgroundColor(Color.WHITE);
                tab6.setBackgroundColor(Color.WHITE);
                break;
            case 1:
                tab2.setBackgroundColor(Color.GRAY);
                tab1.setBackgroundColor(Color.WHITE);
                tab3.setBackgroundColor(Color.WHITE);
                tab4.setBackgroundColor(Color.WHITE);
                tab5.setBackgroundColor(Color.WHITE);
                tab6.setBackgroundColor(Color.WHITE);
                break;
            case 2:
                tab3.setBackgroundColor(Color.GRAY);
                tab2.setBackgroundColor(Color.WHITE);
                tab1.setBackgroundColor(Color.WHITE);
                tab4.setBackgroundColor(Color.WHITE);
                tab5.setBackgroundColor(Color.WHITE);
                tab6.setBackgroundColor(Color.WHITE);
                break;
            case 3:
                tab4.setBackgroundColor(Color.GRAY);
                tab2.setBackgroundColor(Color.WHITE);
                tab3.setBackgroundColor(Color.WHITE);
                tab1.setBackgroundColor(Color.WHITE);
                tab5.setBackgroundColor(Color.WHITE);
                tab6.setBackgroundColor(Color.WHITE);
                break;
            case 4:
                tab5.setBackgroundColor(Color.GRAY);
                tab2.setBackgroundColor(Color.WHITE);
                tab3.setBackgroundColor(Color.WHITE);
                tab4.setBackgroundColor(Color.WHITE);
                tab1.setBackgroundColor(Color.WHITE);
                tab6.setBackgroundColor(Color.WHITE);
                break;
            case 5:
                tab6.setBackgroundColor(Color.GRAY);
                tab2.setBackgroundColor(Color.WHITE);
                tab3.setBackgroundColor(Color.WHITE);
                tab4.setBackgroundColor(Color.WHITE);
                tab5.setBackgroundColor(Color.WHITE);
                tab1.setBackgroundColor(Color.WHITE);
                break;
        }
    }

    private void initUI(){

        GrainCropsFragment grainCropsFragment = new GrainCropsFragment();
        VegetableFragment vegetableFragment = new VegetableFragment();
        FruitTreeFragment fruitTreeFragment = new FruitTreeFragment();
        MedicinalPlantFragment medicinalPlantFragment = new MedicinalPlantFragment();
        ArborFragment arborFragment = new ArborFragment();
        PestFragment pestFragment = new PestFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(grainCropsFragment);
        fragmentList.add(vegetableFragment);
        fragmentList.add(fruitTreeFragment);
        fragmentList.add(medicinalPlantFragment);
        fragmentList.add(arborFragment);
        fragmentList.add(pestFragment);
    }


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public CollectFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment CollectFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CollectFragment newInstance() {
        CollectFragment fragment = new CollectFragment();
        Bundle args = new Bundle();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        initUI();

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_collect, container, false);

        tab1=view.findViewById(R.id.grain_crops);
        tab2=view.findViewById(R.id.vegetable);
        tab3=view.findViewById(R.id.fruit_tree);
        tab4=view.findViewById(R.id.medicinal_plant);
        tab5= view.findViewById(R.id.arbor);
        tab6=view.findViewById(R.id.pest);
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
        tab5.setOnClickListener(this);
        tab6.setOnClickListener(this);
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        viewPager=(ViewPager) view.findViewById(R.id.container);
        viewPager.setAdapter(myFragmentPagerAdapter);
        viewPager.setCurrentItem(0);
        changeTab(0);
       viewPager.addOnPageChangeListener(new MyPageChangeListener());


        return view;
    }

    public class MyPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            changeTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.grain_crops){
            viewPager.setCurrentItem(0);
            changeTab(0);
        }else if(view.getId() == R.id.vegetable){
            viewPager.setCurrentItem(1);
            changeTab(1);
        }else if(view.getId() == R.id.fruit_tree) {
            viewPager.setCurrentItem(2);
            changeTab(2);
        }else if(view.getId() == R.id.medicinal_plant) {
            viewPager.setCurrentItem(3);
            changeTab(3);
        }else if(view.getId() == R.id.arbor) {
            viewPager.setCurrentItem(4);
            changeTab(4);
        }else if(view.getId() == R.id.pest) {
            viewPager.setCurrentItem(5);
            changeTab(5);
        }
    }



}